﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using CondominiosRESTTest.Dominio;

namespace CondominiosRESTTest
{
    [TestClass]
    public class Test01Residente
    {
        [TestMethod]
        public void Test01Crear()
        {
            string postdata = "{\"ApellidoMaterno\":\"Garcia\",\"ApellidoPaterno\":\"Rodriguez\",\"Clave\":\"admin\",\"Correo\":\"cristos@gmail.com\",\"DNI\":\"88888880\",\"Edad\":\"25\",\"Nombres\":\"Cristos\",\"Tipo\":\"R\"}";
            byte[] data = Encoding.UTF8.GetBytes(postdata);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:3000/Residentes.svc/Residentes");

            req.Method = "POST";
            req.ContentLength = data.Length;
            req.ContentType = "application/json";

            var reqStream = req.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            
            var res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());

            string residenteJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();

            TResidente residenteCreado = js.Deserialize<TResidente>(residenteJson);
            Assert.AreEqual("Garcia", residenteCreado.ApellidoMaterno);
            Assert.AreEqual("Rodriguez", residenteCreado.ApellidoPaterno);
            Assert.AreEqual("admin", residenteCreado.Clave);
            Assert.AreEqual("cristos@gmail.com", residenteCreado.Correo);
            Assert.AreEqual("88888880", residenteCreado.DNI);
            Assert.AreEqual(25, residenteCreado.Edad);
            Assert.AreEqual("Cristos", residenteCreado.Nombres);
            Assert.AreEqual("R", residenteCreado.Tipo);
        }

        [TestMethod]
        public void Test02Obtener()
        {
            HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create("http://localhost:3000/Residentes.svc/Residentes/88888888");
            req2.Method = "GET";
            HttpWebResponse res2 = (HttpWebResponse)req2.GetResponse();
            StreamReader reader2 = new StreamReader(res2.GetResponseStream());
            string residenteJson2 = reader2.ReadToEnd();
            JavaScriptSerializer js2 = new JavaScriptSerializer();
            TResidente residenteObtenido = js2.Deserialize<TResidente>(residenteJson2);
            Assert.AreEqual("88888888", residenteObtenido.DNI);
            Assert.AreEqual("cristos@gmail.com", residenteObtenido.Correo);
        }
    }
}
